<?php
namespace app\hsycms\controller;
use think\Controller;
use app\hsycms\controller\Base;

class Seo extends Base
{
	
	//百度推送
    public function baiduurl() {
		$this->assign("v",model('common/Config')->getConfig());
		return $this->fetch(); 
    }	
	
	//网站内链
	public function sitelink(){
		$id = input('id','','intval');
		if($_POST){		
			$data=input("post.");
			if(db('sitelink')->where('title',$data['title'])->find()){
				return json(['status'=>'0','info'=>'关键词已存在']);
			}
			if(db('sitelink')->insert($data)){					
				siteLinkReplace($data);
				return json(['status'=>'y','info'=>'添加成功']);
			}					
		}else{
		  $list = db('sitelink')->order("id desc")->paginate(10,false,['query' => request()->param()]);
		  $page = $list->render();
		  $this->assign("list",$list);
		  $this->assign("page",$page);
		  return $this->fetch();
		}
	}	
	
	//批量删除内链
	public function delsitelink(){
		$id = input('id','','intval');
		$arr = db('sitelink')->where('id',$id)->find();
		delSiteLink($arr);	
		db('sitelink')->where('id',$id)->delete();		
		return json(['status'=>'y','info'=>'删除内链成功']);
	}	
	
	//站点地图
	public function sitemap(){
		$act = input('act');
		if($act=="go"){
			sitemap();
			$this->success('生成成功');
		}else{
			$url = config("weburl");
			$this->assign("url",$url.'sitemap.xml');
			return $this->fetch();
		}
	}
	
	//蜘蛛统计
	public function spider(){
		$title = input('title');
		if($title!=""){
			$where['title'] = $title;
			$this->assign("type",$title);
		}
		$list = db('spider')->where($where)->order('datetime desc')->paginate(10,false,['query' => request()->param()]);
		$page = $list->render();
		$this->assign("list",$list);
		$this->assign("page",$page);
		return $this->fetch();
	}
	
	//删除蜘蛛统计
	public function delspider(){	
		$type = input('type');
		if($type!=""){	
			db('spider')->where('title',$type)->delete();
			return json(['status'=>'y','info'=>'删除成功']);
		}else{
			return json(['status'=>'n','info'=>'请选择要删除的搜索引擎']);
		}
	}
	//友情连接
	public function link(){
		$id = input('id');
		if($_POST){
			$data = input('post.');
			if($id==""){
				if(db('link')->insert($data)){
					return json(['status'=>'y','info'=>'添加成功']);
				}	
			}else{
				if(db('link')->where('id',$id)->update($data)!==false){
					return json(['status'=>'y','info'=>'修改成功']);
				}	
			}
		}else{
			if($id!=""){
				$one = db('link')->where("id",$id)->find();
				$this->assign("v",$one);
			}
			$list = db('link')->paginate(10,false,['query' => request()->param()]);
			$this->assign("list",$list);
			$this->assign("page",$list->render());
			return $this->fetch();
		}
	}
	
	//删除友情连接
	public function dellink(){		
		$id = input('id');
		db('link')->where('id',$id)->delete();
		return json(['status'=>'y','info'=>'删除成功']);
	}
	
	//标签列表
	public function taglist(){		
		if(input('tag')!=""){
			$map['tag'] = array('like','%'.input('tag').'%');
		}	
		$list = db('taglist')->where($map)->order("id desc")->paginate(10,false,['query' => request()->param()]);
		$page = $list->render();
		$this->assign("list",$list);
		$this->assign("page",$page);
		return $this->fetch();
	}	
	
	//TAG SEO设置
	public function tagseo(){
		if($_POST){
			$post=input("post.");
			$data['tag_seo_title'] = $post['tag_seo_title'];
			$data['tag_seo_keywords'] = $post['tag_seo_keywords'];
			$data['tag_seo_description'] = $post['tag_seo_description'];
			$map['name'] = 'tag_seo_title';
			foreach($data as $key=>$v){
				db('config')->where(['name'=>$key])->update(['value'=>$v]);
			}
			return json(['status'=>'y','info'=>'提交成功']);
		}else{
			$this->assign("v",model('common/Config')->getConfig());
			return $this->fetch(); 
		}
	}
	
	//添加TAG
	public function tagadd(){
		$id = input('id','','intval');
		if($_POST){		
			$data=input("post.");
			if($id==0){
				if(db('taglist')->where('tag',$data['tag'])->find()){
					return json(['status'=>'no','info'=>'标签已存在']);
				}
				$data['datetime'] = time();
				if(db('taglist')->insert($data)){
					return json(['status'=>'y','info'=>'添加成功']);
				}
			}else{				
				$oldtag = db('taglist')->where(['id'=>$id])->value('tag');				
				db('taglist')->where(['id'=>$id])->update($data);	
				if($data['tag']!= $oldtag){					
					model('Taglist')->editArcTag($id,$data['tag'],$oldtag); //更新文章TAG
				}
				return json(['status'=>'y','info'=>'编辑成功']);
			}
		}else{
			if($id!=0){
				$v = db('taglist')->where(['id'=>$id])->find();
				$this->assign("v",$v);
			}
			return $this->fetch();
		}
	}
	
	//删除TAG
	public function delTag(){
		$id = input('id');
		$flag = model('taglist')->delTag($id);
		db('taglist')->where('id',$id)->delete();
		return json(['status'=>'y','info'=>'删除成功']);
	}
	
	//TAG关联文章列表
	public function taggl(){
		$tid = input('tid');
		$map['mid'] = array('in','2,3,4,5');
		if(input('title')!=""){
			$map['title'] = array('like','%'.input('title').'%');
		}	
		$list = db('article')->where($map)->order("istop desc,sort desc,id desc")->paginate(20,false,['query' => request()->param()]);		
		//查询已关联文章
		$arc = db('tagnidaid')->where(['tid'=>$tid])->select();
		$aids = [];
		foreach($arc as $key=>$v){
			$aids[] = $v['aid'];
		}
		if(count($aids)>0) {
			$aidsstr = implode('|',$aids);
		}
		$this->assign("tid",$tid);
		$this->assign("aids",$aids);
		$this->assign("aidsstr",$aidsstr);
		$page = $list->render();
		$this->assign("list",$list);
		$this->assign("page",$page);
		return $this->fetch();
	}
	
	//关联文章
	public function pltaggl(){
		if($_POST){			
			$tid  = input('tid'); //tag ID
			$tag  = db('taglist')->where(['id'=>$tid])->value('tag');
			$oldid = input('aids');//已关联文章ID
			$newidarr = [];
			foreach($_POST['id'] as $v){ $newidarr[] = $v; }
			model('Taglist')->glArcTag($tid,$tag,$oldid,$newidarr);
			$this->success("标签关联成功",url('taglist'));
		}
	}

}